/*
 * Copyright 2010 Gaurav Saxena
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwtstructs.gwt.client.widgets.stylesheets;


public class CssImpl {
	/**
	 * @param ruleName string modelled as a css selector e.g. #id, .styleClass, div etc
	 * @param styleAttribute standard style attrributes as used in javascript like backgroundColor, fontSize
	 * , height etc 
	 * @param attributeValue value of the style attribute
	 * @return true if a rule is modified, false otherwise
	 */
	public native boolean editRule(String ruleName, String styleAttribute, String attributeValue) /*-{
		for(var i = 0, len = $doc.styleSheets.length; i < len; i++) {
			var cssrules = $doc.styleSheets[i].cssRules;
			if(editFromAllCssRule(cssrules, ruleName, styleAttribute, attributeValue))
				return true;
		}
		return false;
				
		function editFromAllCssRule(cssrules, ruleName, styleAttribute, attributeValue){
			for(var i = 0; i < cssrules.length; i++) {
				if(cssrules[i].selectorText.indexOf('@import') > -1 && editFromAllCssRule(cssrules[i].styleSheet.cssRules, ruleName, styleAttribute, attributeValue))
					return true;
				else if(cssrules[i].selectorText.toLowerCase().indexOf(ruleName.toLowerCase()) > -1) {
					cssrules[i].style[styleAttribute] = attributeValue;
					return true;
				}
			}
			return false;
		}
	}-*/;
	/**
	 * @param ruleName string modelled as a css selector e.g. #id, .styleClass, div etc
	 * @return the complete css rule e.g. div {background: red} if rule is found, else empty string
	 */
	public native String getRule(String ruleName)/*-{
		for(var i = 0, len = $doc.styleSheets.length; i < len; i++) {
			var cssrules = $doc.styleSheets[i].cssRules;
			var cssRule = getFromAllCssRules(cssrules, ruleName);
			if(cssRule != "")
				return cssRule;
		}
		return "";
		function getFromAllCssRules(cssrules, ruleName) {
			for(var i = 0; i < cssrules.length; i++) {
				if(cssrules[i].selectorText.indexOf('@import') > -1) {
					var cssRule = getFromAllCssRules(cssrules[i].styleSheet.cssRules, ruleName);
					if(cssRule != "")
						return cssRule;
				}
				else if(cssrules[i].selectorText.toLowerCase() == ruleName.toLowerCase())
					return cssrules[i].cssText;
			}
			return "";
		}
	}-*/;
	/**
	 * @param ruleName string modelled as a css selector e.g. #id, .styleClass, div etc
	 * @param ruleBody string which provides css style rules e.g. color: red; background: yellow;
	 */
	public native void addRule(String ruleName, String ruleBody)/*-{
		this.@com.gwtstructs.gwt.client.widgets.stylesheets.CssImpl::deleteRule(Ljava/lang/String;)(ruleName);
		$wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].insertRule(ruleName + '{' + ruleBody + '}'
		, $wnd.document.styleSheets[0].cssRules.length)
	}-*/;
	/**
	 * @param ruleName string modelled as a css selector e.g. #id, .styleClass, div etc
	 * @return true if the rule was found and deleted, false otherwise
	 */
	public native boolean deleteRule(String ruleName)/*-{
		var cssrules = $wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].cssRules;
		for(var i = 0; i < cssrules.length; i++)
		{
			if(cssrules[i].selectorText.toLowerCase() == ruleName.toLowerCase())
			{
				$wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].deleteRule(i);
				return true;
			}
		}
		return false;
	}-*/;
}
